package com.pudu.mydemo.entity;

import android.os.Message;
import android.util.Log;

import com.pudu.mydemo.utils.ConstantUtils;
import com.pudu.pdrobot.App;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by lgq on 2016/7/13.
 */
public class TcpReceiverThread implements Runnable {

    private ServerSocket serverSocket;
    Socket socket;
    BufferedReader in;
    String source_address;
    private boolean flag = true;

    @Override
    public void run() {

        serverSocket = null;
        socket = null;
        in = null;
        try {

            serverSocket = new ServerSocket(ConstantUtils.TCP_PORT);
            Log.i("App","TcpReceiverThread start");
            while (flag) {
                Log.d("App","TcpReceiverThread ");
                socket = serverSocket.accept();

                if (socket != null) {
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    sb.append(socket.getInetAddress().getHostAddress());
                    StringBuilder sbReceiveData = new StringBuilder();
                    //读取接收的数据
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        sbReceiveData.append(line);
                    }
                    source_address = sb.toString().trim() + sbReceiveData.toString().trim();
                    //处理接收到的数据
                    Message msg = new Message();
                    msg.what = 0x222;
                    String information = "收到来自: " + "\n" + source_address + "\n" + "的tcp请求\n\n";
                    Log.i("App","information::" + information);

                    String strData = sbReceiveData.toString().trim();
                    String strType = getMessageType(strData);
                    Log.d("App", "information  strData::" + strData);
                    if (strType.compareTo("MAP") == 0) {
                        strData = strData.substring("TYPE:MAP:".length());
                        Log.i("lgq", "information::" + strData);
                        App.getInstance().saveApplicationMapData(strData);
                    } if (strType.compareTo("ATLAS") == 0) {
                        strData = strData.substring("TYPE:ATLAS:".length());
                        Log.i("ykl", "information::" + strData);
                        App.getInstance().saveApplicationAtlasData(strData);
                    } else {
                        //other type
                    }
                    Log.d("ykl", "information  otherType::" + strType);
                }
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            if (!flag) {
                try {
                    if (in != null)
                        in.close();
                    if (socket != null)
                        socket.close();
                    if (serverSocket != null)
                        serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public String getMessageType(String strData) {
        String strType = "";
        try {
            String strTemp = strData.substring("TYPE:".length());
            int indexNode = strTemp.indexOf(":");
            strType = strTemp.substring(0, indexNode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strType;
    }

}
